"""安装：
pip3 install plotly --break-system-packages
pip3 install pandas --break-system-packages
plotly依赖 pandas
"""
# 查看 GeoJSON 文件，处理 json 文件
from pathlib import Path
import json

    #将数据作为字符串读取并转为 python 对象
path = Path("../files/eq_data_1_day_m1.geojson")
contents = path.read_text()
print(type(contents))
#print(contents)
all_eq_data = json.loads(contents)
print(type(all_eq_data))
#print(all_eq_data)
    #将数据文本转为更易阅读的版本
path = Path("../files/readable_data_1_day_m1.geojson")
readable_contents = json.dumps(all_eq_data,indent = 4)
print(type(readable_contents))
path.write_text(readable_contents)

# 创建地震列表   包含所有地震各种信息
from pathlib import Path
import json
path = Path("../files/eq_data_30_day_m1.geojson")
contents = path.read_text()
all_eq_data = json.loads(contents)
#print(all_eq_data)
    ## 查看数据中的所有地震
all_eq_dicts = all_eq_data['features']
#print(all_eq_dicts)
print(len(all_eq_dicts))
print(type(all_eq_dicts))

# 提取震级
mags = []
for eq_dict in all_eq_dicts:
    mag = eq_dict["properties"]["mag"]
    mags.append(mag)
print(mags[:10])

# 提取位置数据
mags, titles, lons, lats = [],[],[],[]
for eq_dict in all_eq_dicts:
    mag = eq_dict["properties"]["mag"]
    title = eq_dict["properties"]["title"]
    lon = eq_dict["geometry"]["coordinates"][0]
    lat = eq_dict["geometry"]["coordinates"][1]
    mags.append(mag)
    titles.append(title)
    lons.append(lon)
    lats.append(lat)
print(mags[:10])
print(titles[:2])
print(lons[:5])
print(lats[:5])
print(len(lons))
print(len(lats))

# 绘制地震散点图
import plotly.express as px
print(len(lons))
print(lons)
print(len(lats))
print(lats)
fig = px.scatter(
    x=lons,
    y=lats,
    labels={"x": "经度", "y": "纬度"},
    range_x=[-200, 200],
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图"
)
fig.write_html("../files/global_earthquakes.html")
fig.show()

# 指定其他数据  使用 pandas
import pandas as pd

data = pd.DataFrame(data=zip(lons,lats,titles,mags), columns = ["经度","纬度","位置","震级"])
print(data.head())
fig = px.scatter(data,x="经度",y="纬度")
fig.write_html("../files/global_earthquakes2.html")
fig.show()

# 定制标记的标尺  要在图中指出震级，设置点的大小不同来看
fig = px.scatter(
    data,
    x='经度',
    y='纬度',
    range_x=[-200, 200],
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图",
    size="震级",
    size_max=10,
)
fig.write_html("../files/global_earthquakes3.html")
fig.show()

# 定制标记颜色
fig = px.scatter(
    data,
    x='经度',
    y='纬度',
    range_x=[-200, 200],
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图",
    size="震级",
    size_max=10,
    color="震级",
)
fig.write_html("../files/global_earthquakes4.html")
fig.show()

# 其他渐变
import plotly.express as px
print(px.colors.named_colorscales())

fig = px.scatter(
    data,
    x='经度',
    y='纬度',
    range_x=[-200, 200],
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图",
    size="震级",
    size_max=10,
    color="震级",
    color_continuous_scale="greens"
)
fig.write_html("../files/global_earthquakes4.html")
fig.show()

# 添加悬停文本
fig = px.scatter(
    data,
    x='经度',
    y='纬度',
    range_x=[-200, 200],
    range_y=[-90, 90],
    width=800,
    height=800,
    title="全球地震散点图",
    size="震级",
    size_max=10,
    color="震级",
    color_continuous_scale="greens",
    hover_name="位置"
)
fig.write_html("../files/global_earthquakes4.html")
fig.show()